import tornado.web
from tornado.options import options
import datetime
from PyWebsite3.common import *


# 展示名基金页面
class RenderTopManagerHandler(tornado.web.RequestHandler):
    def get(self, path):
        invest_type = path
        database = options.database
        datetime_update = datetime.datetime.now()

        max_date = database.ExecuteSQL("derivative_data",
                                       "Select max(date) from derivative_data.fund_manager_score")
        recent_date = max_date[0][0]

        # 读取基金快照表
        manager_filter = [("invest_type", invest_type), ("date", recent_date), ("limit", 100)]
        manager_documents = database.Find("derivative_data", "fund_manager_score", filter=manager_filter, sort=[("score", -1)])

        for document in manager_documents:
            float_formatted(document, fields=["value",
                                              "excess_return_pct",
                                              "sharpe_ratio", "sharpe_ratio_pct",
                                              "information_ratio", "information_ratio_pct",
                                              "annualized_volatility_pct",
                                              "manage_days", "manage_days_pct",
                                              "aum", "aum_pct"], keep_float=2)

            float_2_percentage(document, fields=["excess_return", "annualized_volatility"])
        #
        self.render("top_manager.html", manager_documents=manager_documents, invest_type=invest_type)
